import org.zyg.baseFunc

def call() {
    def baseFunc = new baseFunc()
    def job_objcet = baseFunc.getMysqlMsg(env.JOB_BASE_NAME)[0]
    // git仓库地址
    env.PROJECT_GIT_URL =  "${job_objcet.git_url}"
    // 项目类型
    env.PROJECT_TYPE = "${job_objcet.job_type}"
    // 项目的端口
    env.PORT = "${job_objcet.job_port}"
    // 项目所在的路径
    env.PROJECT_PATH = "${job_objcet.project_path}"
    println """
    项目名称： ${env.JOB_BASE_NAME}
    仓库地址： ${env.PROJECT_GIT_URL}
    项目类型： ${env.PROJECT_TYPE}
    项目端口： ${env.PORT}
    制品路径： ${env.PROJECT_PATH}
    """
    pipeline {
        agent any
        tools {
            maven "mvn3.3"
        }
        parameters {
            listGitBranches(
                name: 'BRANCH', 
                branchFilter: 'refs/heads/(.*)', 
                credentialsId: 'git-root', 
                defaultValue: 'master', 
                description: '你要选取哪个分支进行构建', 
                quickFilterEnabled: true, 
                remoteURL: env.PROJECT_GIT_URL, 
                selectedValue: 'NONE', 
                sortMode: 'NONE', 
                tagFilter: '*', 
                type: 'PT_BRANCH')
            choice(name: 'PLATFORM', choices: ['dev','qa'], description: '要部署哪个环境')
        }
        stages {
            stage('获取用户信息') {
                steps {
                    script {
                        wrap([$class: 'BuildUser']){
                            env.BUILD_USER = "${env.BUILD_USER_ID}" // 当次构建的用户 拼音
                            env.BUILD_USER_FIRST_NAME = "${env.BUILD_USER_FIRST_NAME}" // 当次构建用户 中文名字
                            env.BUILD_USER_EMAIL = "${env.BUILD_USER_EMAIL}" // 当次构建用户 邮箱
                        }
                    }
                }
            }
            stage('代码检出') {
                steps {
                    script {
                        baseFunc.gitCheckOut(env.PROJECT_GIT_URL, env.BRANCH)
                    }
                }
            }
            stage('构建') {
                steps {
                    script {
                        baseFunc.javaBuild(env.PROJECT_PATH)
                    }
                }
            }
            stage('部署') {
                steps {
                    script {
                        baseFunc.javaDeploy(env.PLATFORM, env.JOB_BASE_NAME, env.PORT)
                    }
                }
            }
        }
        post {
            always {
                script{
                    baseFunc.dingMsgSend(params.PLATFORM, params.PLATFORM, JOB_BASE_NAME, env.BUILD_URL, params.BRANCH, env.BUILD_USER_FIRST_NAME,currentBuild.result)
                    currentBuild.displayName = "# ${BUILD_NUMBER}-${BRANCH}-${BUILD_USER}-${PLATFORM}"
                    currentBuild.description = "仓库：${env.PROJECT_GIT_URL} <br>分支：${BRANCH} <br>构建人： ${BUILD_USER}"
                }
            }
        }
    }
}